use "C:\Users\mcsam\Dropbox\Decomposing Firm Betas\Submissions\JPE Macro\Replication Folder\Table_A1_data",clear

/*ony keep industries where we have at least one match*/
forvalues i=1/5{
	gen n`i'=.
}
local z=1
forvalues i=1/10{
	preserve
	keep if cnum==`i'
	bysort year quarter: ///
		egen minsamp=min(oursamp)
	bysort year quarter: ///
		egen maxsamp=max(oursamp)
	keep if minsamp==0 & maxsamp==1
	/*
	bysort sic`i' year quarter: egen tmatches=total(oursamp)
	keep if tmatches>=10
	*/

	bysort year quarter (revtq): ///
		gen rank=_n 
	bysort year quarter (revtq): ///
		gen obs=_N
	gen rank`i'=obs-rank+1	
	*gen prank`i'=rank`i'/obs	
	gen prank`i'=rank/obs	
	

	sum rank`i' if oursamp==1 
	local n1=`r(mean)'
	sum rank`i' if oursamp==0
	local n2=`r(mean)'

	sum prank if oursamp==1 
	local n3=`r(mean)'
	sum prank if oursamp==0
	local n4=`r(mean)'

	keep if rank`i'<=5

	gen cter=1
	bysort year quarter: egen t1=total(oursamp)
	bysort year quarter: egen t2=total(cter)
	gen sharet5=t1/t2
	keep sharet5 year quarter
	duplicates drop
	sum sharet5 
	local n5=`r(mean)'

	restore
	forvalues j=1/5{
		replace n`j'=`n`j'' if _n==`z'
	}
	local z=`z'+1
}
keep n1-n5
drop if n1==.
